package cn.wolfcode.wolf2w.redis.service.impl;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Service
public class StrategyStatisVoRedisServiceImpl implements IStrategyStatisVoRedisService {
    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyService strategyService;
    @Override
    public void viewnumIncrease(String sid, int num) {
        //判断vo对象是否存在
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);//抽取重复

        //viewnum + num
        vo.setViewnum(vo.getViewnum() + num);
        //更新vo对象
        this.setStrategyStatisVo(vo);
    }

    @Override
    public StrategyStatisVO getStrategyStatisVo(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //判断vo对象是否存在
        StrategyStatisVO vo = null;
        if (!template.hasKey(key)){
            //如果不存在,初始化vo
            vo = new StrategyStatisVO();

            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());

            template.opsForValue().set(key, JSON.toJSONString(vo));
        }else {
            String voStr = template.opsForValue().get(key);
            vo = JSON.parseObject(voStr,StrategyStatisVO.class);

        }

        return vo;
    }

    @Override
    public void setStrategyStatisVo(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key, JSON.toJSONString(vo));
    }

    @Override
    public void replynumIncrease(String sid, int num) {
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        vo.setReplynum(vo.getReplynum()+num);
        this.setStrategyStatisVo(vo);
    }

    @Override
    public boolean favor(String sid, String uid) {
        //获取记号
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        List<String> sidList = this.getUserStrategyFavor(uid);//抽取重复
        //通过记号判断当前操作是收藏还是取消收藏
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        if (sidList.contains(sid)){
            //取消收藏,-1
            vo.setFavornum(vo.getFavornum()-1);
            sidList.remove(sid);
        }else {
            //收藏成功,+1
            vo.setFavornum(vo.getFavornum()+1);
            sidList.add(sid);
        }
        //更新记号和vo
        template.opsForValue().set(key, JSON.toJSONString(sidList));
        this.setStrategyStatisVo(vo);

        return sidList.contains(sid);
    }

    @Override
    public List<String> getUserStrategyFavor(String uid) {

        //获取记号
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //记号不存在,初始化
        List<String> sidList = new ArrayList<>();
        //判断记号是否存在
        if (template.hasKey(key)){
            //存在,直接获取
            String str = template.opsForValue().get(key);

            //参数2:集合泛型
            sidList = JSON.parseArray(str, String.class);
        }
        return sidList;
    }

    @Override
    public boolean strategyThumbup(String sid, String uid) {

        //获取记号
        String key = RedisKeys.USER_STRATEGY_THUMBUP.join(uid, sid);
        //判断key是否存在
        if(!template.hasKey(key)){
            //不存在点赞数+ 1,把记号添加到redis中,更新vo对象
            StrategyStatisVO vo = this.getStrategyStatisVo(sid);
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            this.setStrategyStatisVo(vo);

            //设置有效时间-当前时间
            Date now = new Date();
            Date end = DateUtil.getEndDate(now);  //今天最后一秒时间对象
            long time = DateUtil.getDateBetween(now, end);  //有效时间， 单位秒
            template.opsForValue().set(key, "1",time,TimeUnit.SECONDS);
            return true;
        }
        return false;
    }

    @Override
    public boolean isStrategyVoExist(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);

        return template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> queryStrategyVos() {
        //拼接redis的key
        String pattern = RedisKeys.STRATEGY_STATIS_VO.join("*");

        Set<String> keys = template.keys(pattern);

        List<StrategyStatisVO> list = new ArrayList<>();
        if(keys != null && keys.size() > 0){
            for (String voKey : keys) {
                String voStr = template.opsForValue().get(voKey);
                list.add(JSON.parseObject(voStr, StrategyStatisVO.class));
            }
        }
        return list;
    }

}
